<?php

//管理员商品栏目
if (!defined('IN_T')) {
    die('hacking attempt');
}

$id = get_company_id();
//查看栏目列表
if ($act == "index") {
    $res = productCatList();
    $tp->assign('list', $res);
} //编辑商品分类
else if ($act == "detail") {
    $company_new_cat_id = intval($_REQUEST['company_new_cat_id']);
    //验证$company_new_cat_id是否合法
    $company_new_cat = $Db->query("SELECT * FROM " . $Base->table('company_admin_new_cat') . " WHERE id = $company_new_cat_id", "Row");
    if (empty($company_new_cat)) {
        $company_new_cat_id = 0;
    }
    //前台显示
    if (empty($_POST)) {
        //编辑
        $tp->assign('company_new_cat', $company_new_cat);

    } //后台数据处理
    else {
        $res['status'] = 0;
        $data = array(
            'name' => Common::sfilter($_POST['name']),
            'sort' => intval($_POST['sort']),
            'icon' => Common::sfilter($_POST['icon']),
        );

        if (empty($data['name'])) {
            $res['msg'] = '分类名称不能为空';
        } else {
            if ($company_new_cat_id) {
                $Db->update($Base->table('company_admin_new_cat'), $data, array('id' => $company_new_cat_id));
            } else {
                $company_new_cat_id = $Db->insert($Base->table('company_admin_new_cat'), $data);
            }
            $res = array('status' => 1, 'msg' => '提交成功', 'href' => '/' . COMPANY_PATH . '/?m=new_cat');
        }
        echo $Json->encode($res);
        exit;
    }
} //删除栏目分类
else if ($act == "delete") {
    $id = intval($_POST['company_new_cat_id']);
    // var_dump($id);
    $res['status'] = 0;
    //检测商家是否在使用
    $sql = "select * from ".$Base->table('company_new_cat')." where pid=".$id;
    $resu = $Db->query($sql);
    if(empty($resu)){
        //删除分类
        $Db->execSql("delete from " . $Base->table('company_admin_new_cat') . " where id=$id ");
        $res['status'] = 1;
    }else{
        $res['msg'] = "有商家在使用分类,不能删除";
    }
    echo $Json->encode($res);
    exit;
}

$tp->assign('nav', '栏目管理');
$tp->assign('act', $act);

function productCatList()
{
    $sql = "SELECT * FROM " . $GLOBALS['Base']->table('company_admin_new_cat') . " ORDER BY  sort asc , id asc";
    $list = $GLOBALS['Db']->query($sql);
    return array('list' => $list);
}

?>